package com.clickpaas.materialsprocurement.service.Impl;

import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.model.STDmaterielDTO;
import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.model.STDmaterielDTOPageInfoResponse;
import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.model.STDmaterielDTOResponse;
import com.clickpaas.materialsprocurement.dao.MaterialsDao;
import com.clickpaas.materialsprocurement.entity.MaterialsEntity;
import com.clickpaas.materialsprocurement.service.MaterialsService;
import com.clickpaas.materialsprocurement.utils.DataInfoUtil;
import com.clickpaas.materialsprocurement.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class MaterialsServiceImpl implements MaterialsService {

    @Resource
    private MaterialsDao materialsDao;
    /**
     * 获取所有物料数据
     *
     * @return
     */
    @Override
    public List<STDmaterielDTO> getMaterialsInfo(MaterialsEntity materialsEntity) {
        List<STDmaterielDTO> materielList;
        try {
            /**
             * 入参为空，全表查询
             */
            if (null == materialsEntity || DataInfoUtil.isAllFieldNull(materialsEntity)){
                materielList = DataInfoUtil.castListCopy(materialsDao.queryMaterialsInfo(), STDmaterielDTOPageInfoResponse.class,STDmaterielDTO.class);
            }else {
                materielList = DataInfoUtil.castListCopy(materialsDao.queryMaterialsInfo(materialsEntity), STDmaterielDTOResponse.class,STDmaterielDTO.class);
            }
        } catch (Exception e) {
            log.info("入参对象异常,请检查：【{}】,报错信息：【{}】",materialsEntity,e);
            return null;
        }
        if (null != materielList && materielList.size()>0){
            return materielList;
        }
        return null;
    }

    @Override
    public List<MaterialsEntity> MaterialDataListConver(List<STDmaterielDTO> materList){
        return DataInfoUtil.castListCopy(materList,STDmaterielDTO.class,MaterialsEntity.class);
    }

    @Override
    public MaterialsEntity MaterialDataConver(STDmaterielDTO mater){
        MaterialsEntity materialsEntity = new MaterialsEntity();
        SpringUtil.copyPropertiesIgnoreNull(mater,materialsEntity);
        return materialsEntity;
    }

    /**
     * 物料数据入库
     *
     * @param materialsEntity
     * @return
     */
    @Override
    public boolean insertMaterialInfo(MaterialsEntity materialsEntity) {
        if (null == materialsEntity)return false;
        return materialsDao.insertMaterialsInfo(materialsEntity);
    }
}
